﻿using System;
using Microsoft.VisualStudio.TestTools.UnitTesting;
using FODATAC;
using System.Net;
using PISON.SharedDomain;
using FODATAC.Interfaces;
using System.Linq;
using PIWorks.Base.SharedDomain;
using System.Collections.Generic;
using PIWorks.ObjectModel.Domain;
namespace FODATAC.Test
{
	[TestClass]
	public class UnitTest1
	{
		[TestMethod]
		public void TestMethod1()
		{

			var uri = new Uri("http://10.10.10.144:9991/ExecutionService/OData/");


			//var odata = new LinqProvider.LinqProvider("http://10.10.10.64:9991/ExecutionService/OData");

			//var plans = odata.CreateQuery<ExecutionPlan>("ExecutionPlans").Where(k => k.Name == "a").ToList();


			ISessionFactory sf = new SessionFactory(uri, "admin", "123");
			sf.MetaData.SetCollectionName(typeof(RegisteredOptimizer), "Optimizers");
			var session = sf.OpenSession();
			/*
			var exe1 =session.Query<Execution>().FirstAsync ( k => k.Id == 141216).Result;
			var uuu = session.GetEntityUri(exe1);
			var sol2 = new Solution() { Execution = exe1,  MimeType = "", Name = "", Report = new byte[0]};
			session.SaveAsync(sol2).Wait();
			var sol = session.Query<Solution>().FirstAsync().Result;
			var uri2 = session.GetEntityUri(sol);
			var feq = new NetworkObject(new PrimaryKey(301,0,0,0),301);
			var res = session.CreateQuery(uri.AbsoluteUri + "/GetFrequencyUsage").PostAsync<string>(new List<BodyParameter>() { new BodyParameter("TargetArea", new[] { feq }) }).Result;
			*/
			//var res = session.Query<Solution>().Where(r => r.Execution.Id != 12131).FirstAsync().Result;
				//.Take(2)/*.Fetch(k => k.ExecutionPlan.CreatedBy)*/
				//.Select(r => r.Suggestions).ToListAsync().Result;
			/*
			var plans = session.CreateQuery("http://10.10.10.144:9991/ExecutionService/OData/Solutions?$top=10&$expand=Execution").ListAsync<Solution>().Result;
			plans[0].Name = "onur";
			session.Delete(plans[0]);*/
			var exe2 = session.Query<ExecutionPlan>().FirstAsync(k => k.Id == 144135).Result;
			var exe = session.Query<Execution>().FirstAsync(k => k.Id == 144135).Result;
			session.LoadAssociationAsync(exe, c => c.ExecutionPlan).Wait();
			//session.LoadAssociationAsync(exe, c => c.Optimizers).Wait();
			session.Evict(exe);
			exe = session.MergeAsync(exe).Result;


		//	var sug = session.Query<RegisteredOptimizer>().FirstAsync().Result;
			//exe.Optimizers.Remove(exe.Optimizers.First());
			
			//exe.Optimizers.Add(sug);
			//sol.Optimizers.Add(sug);*/
			//var ep = new ExecutionPlan();
			/*
			var ep = session.Query<ExecutionPlan>().FirstAsync().Result;
			ep.Name = ep.Name + "s";
			ep.DateIntervals.Add(new DateInterval(DateTime.MinValue, DateTime.MinValue));
			session.FlushAsync().Wait();
			//sol.Suggestions.Add(sug);*/
			
		//	session.SaveAsync(ep).Wait();

				/*
			var profile = new Profile(user);
			profile.Name = "onur";
			session.SaveAsync(profile).Wait();*/

			session.FlushAsync().Wait();
		}
	}
}
